package com.gosophia.conferenceManagement.jbpm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gosophia.conferenceManagement.ConferenceConfiguration;
import com.gosophia.conferenceManagement.common.entity.ConferenceJobData;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.JobData;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.taskManagement.service.impl.AbstractConfForkNodeActionHandler;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

/**
 * 处理会务准备分支节点的ActionHandler <br/>
 * 
 * @author Tong
 *
 */
@Component
public class ConferenceAffairStartActionHandler extends
        AbstractConfForkNodeActionHandler {

    private static final long serialVersionUID = 1L;
    private final Logger logger =Logger.getLogger(this.getClass());
    
    @Autowired
    private HashMap<String,NodeConfiguration> conferenceConfiguration;
    @Autowired
    private UserManagementService userManagementService;
	@Override
	public List<Job> createNewJobsAfterEnteringNode(Mission mission,
	        String nodeName, String operationName, JobData jobData) {
		
	    logger.debug("进入【"+nodeName+"】"+"节点");
		Date now = new Date();
		NodeConfiguration nodeConfiguration = conferenceConfiguration.get(nodeName);
		ConferenceJobData conferenceJobData = (ConferenceJobData)jobData;
		
		List<Job> jobs= new ArrayList<Job>();
		List<JobConfiguration> jobConfigurations = conferenceConfiguration.get(nodeName).getJobConfs();
	    
		String bsSection=nodeConfiguration.getBsSection();
		
		for (JobConfiguration jobConfiguration : jobConfigurations) {
		  // 创建预通知起草任务、通知起草任务、会前准备任务
		     UserInfo jobUnderTaker = dynamicJobUnderTaker(mission,bsSection,jobConfiguration,conferenceJobData);//任务承办人
		     UserInfo jobSource = getJobSource(mission,conferenceJobData);//任务来源
		     Job job = createJob(
		                jobConfiguration.getJobType(), jobConfiguration.getJobState(), 
		                now, mission.getScheduleEndDate(), 
		                jobUnderTaker, 
		                jobSource, 
		                Job.JOB_AFTERFINISH_NORMAL, 
		                jobConfiguration.getDetailURL(), 
		                jobConfiguration.getHandlerURL()
		                );
		        jobs.add(job);
        }
		for (Job job : jobs) {
		    logger.debug("【"+nodeName+"节点】"+"  actionHandler处理,"+"创建 job jobType：["+ job.getJobType() +"]"
                    +"  job 来自：[id:"+job.getSource().getUserInfoId()+",name:"+job.getSource().getUserName()
                    +"] job承办人：[id:"+job.getUndertaker().getUserInfoId()+",name:"+job.getUndertaker().getUserName()+"]"
            );
        }
		return jobs;
	}
	
	//此方法暂时能够满足业务需求，可以修正使用动态处理类绑定的方法，处理不同情况下的任务承办人分配
    private UserInfo dynamicJobUnderTaker(Mission mission,String bsSection,JobConfiguration jobConf,ConferenceJobData prejobData){
        //根据节点，如果是审批节点，就是jobData中的下一个审批人，其他节点统统都是大任务的承办人
        if(jobConf.isGetBackAble()){//取回的任务
        	return mission.getUndertaker();
            //return obtainConferenceAssignUnderTaker(mission,bsSection);
        }else if (prejobData!=null && prejobData.getNextApproval()!=null){
              return prejobData.getNextApproval();//审批任务的承办人
          }
        else {
        	return mission.getUndertaker();
             //return obtainConferenceAssignUnderTaker(mission,bsSection);
          }
    }
         
    //获取会议申请中指定的会议承办人
//    private UserInfo obtainConferenceAssignUnderTaker(Mission mission,String bsSection){
//        Object conf_assign = getCurrentContext().getVariable(ConferenceConfiguration.CONFERENCE_APP_ASSIGN);
//        if(conf_assign!=null && !"会议申请环节".equals(bsSection) ){
//            logger.debug("会议申请环节后的成本人："+(conf_assign));
//            return userManagementService.findUserInfoByUserInfoId((Long)conf_assign);
//        }else{
//            return mission.getUndertaker();
//        }
//    }
    //获取任务来源
    private UserInfo getJobSource(Mission mission,ConferenceJobData jobData){
        if(jobData==null){
            return  mission.getCreator();
        }
        return jobData.getJob().getOperator();
    }

}
